//
//  ViewModel.swift
//  eBooks
//
//  Created by JunMing on 2019/11/15.
//  Copyright © 2019 赵俊明. All rights reserved.
//

import Foundation
import RxSwift
import SwiftyJSON

class MyReaderModel {
    let modelObserable:Variable<[Model]> = {
        var dataSource:[Model] = SQLHelper.share.fetchLocalDB(queryType: .totalTime)
        return Variable(dataSource)
    }()
}

class ViewModel {
    let modelObserable:Variable<[Model]> = {
        var dataSource:[Model] = SQLHelper.share.fetchLocalDB(queryType: .deleteFalse)
        return Variable(dataSource)
    }()
}

class SetViewModel {
    let modelObserable:Variable<[SetHeaderModel]> = {
        let path = Bundle.main.path(forResource: "setConfig", ofType: "json")
        let data = try! Data(contentsOf: URL(fileURLWithPath: path!),options: .mappedIfSafe)
        let json = try! JSON(data: data, options: .allowFragments)
        var setModels:[SetHeaderModel] = [SetHeaderModel]()
        for (index,subJSON):(String,JSON) in json {
            for (key,value):(String,JSON) in subJSON {
                let model = SetHeaderModel("downloadBooks", key)
                setModels.append(model)
                for (subIndex,dicInfo):(String,JSON) in value {
                    let pushVC = dicInfo["pushVC"].string
                    let subModel = SetModel(
                        dicInfo["icon"].string,
                        dicInfo["title"].string,
                        dicInfo["accType"].string,
                        dicInfo["adType"].string)
                    subModel.pushVC = dicInfo["pushVC"].string
                    model.items.append(subModel)
                }
            }
        }
        return Variable(setModels)
    }()
}
